/**
 *
 */
package demo.tellurium.module.cqp

import org.apache.commons.lang.math.RandomUtils;
import org.telluriumsource.ui.object.RadioButton;

import groovy.lang.MetaClass;
import demo.tellurium.framework.BaseUIModule;
import demo.tellurium.framework.BaseUIModule.OfferingTypeOptionIndex 

/**
 * @author roger.xu
 *
 */
public class CQPProfileModule extends BaseUIModule {

    private static final String MODULE_NAME = "CQPProfile";
    public static final String PATH_KEY = "cqp.profile.path";

    @Override
    public String getModuleName() {
        return MODULE_NAME;
    }

    @Override
    public void defineUi() {
        ui.Container(uid: "CQPProfile") {
            // OfferingType
            DOJO_ComboBox(uid: "OfferingType", widgetid: "offeringType")
            // Division
            DOJO_ComboBox(uid: "Division", widgetid: "division")
            // StackName
            DOJO_ComboBox(uid: "StackName", widgetid: "stackName")
            // ProductName
            DOJO_AutoCompleteTextBox(uid: "ProductName", widgetid: "productName")
            // VRMF
            DOJO_TextBox(uid: "VRMF", clocator: [widgetid: "VRMF"])
            // ProductType
            DOJO_TooltipDialogTextBox(uid: "ProductType", widgetid: "productType")
            // Platform
            DOJO_TooltipDialogTextBox(uid: "Platform", widgetid: "documentPlatform")
            // Phase
            DOJO_ComboBox(uid: "Phase", widgetid: "phase")
            // AssessmentType
            Container(uid: "AssessmentType") {
                DOJO_RadioButton(uid: "Enterprise", clocator: [widgetid: "assessmentType_enterprise"])
                DOJO_RadioButton(uid: "MidMarket", clocator: [widgetid: "assessmentType_midMarket"])
            }
            // Role
            DOJO_ComboBox(uid: "Role", widgetid: "role")
            // DocumentName
            DOJO_TextBox(uid: "DocumentName", clocator: [widgetid: "documentName"])
            // Description
            InputBox(uid: "Description", clocator: [tag: "textarea", id: "description"])
            // GrantUser
            Container(uid: "GrantUser") {
                Container(uid: "TextBox") {
                    Container(uid: "InputField") {
                        InputBox(uid: "InputTextBox", clocator: [tag: "input", type: "text", id: "gausers"], respond: ["keyPress"])
                    }
                }
                Container(uid: "PopUpList", clocator: [tag: "div", class: "bpac"]) {
                    List(uid: "Options", clocator: [tag: "ul"], separator: "li") {
                        UrlLink(uid: "{all}", respond: ["mouseDown", "mouseUp", "mouseOver"])
                    }
                }
            }
            // BlueGroup
            DOJO_AutoCompleteTextBox(uid: "BlueGroup", widgetid: "bluegroup")
            Button(uid: "ResetButton", clocator: [id: "resetButton"])
            Button(uid: "SaveButton", clocator: [id: "saveButton"])
            Button(uid: "NextButton", clocator: [id: "nextButton"])
            Button(uid: "CancelButton", clocator: [id: "cancelButton"])
        }
    }

    /* ---- ComboBox ---------------*/
    public void doSelectOfferingType(int index) {
        def widgetName = "OfferingType";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);
        doSelectComboBoxItem(widgetUid, index);
    }

    public void doSelectDivision(int index) {
        def widgetName = "Division";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);
        doSelectComboBoxItem(widgetUid, index);
    }

    public void doSelectStackName(int index) {
        def widgetName = "StackName";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);
        doSelectComboBoxItem(widgetUid, index);
    }

    public void doSelectPhase(int index) {
        def widgetName = "Phase";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);
        doSelectComboBoxItem(widgetUid, index);
    }

    public void doSelectRole(int index) {
        def widgetName = "Role";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);
        doSelectComboBoxItem(widgetUid, index);
    }

    /* ---- TooltipDialogTextBox ---------------*/
    public void doSelectProductType(int index) {
        def widgetName = "ProductType";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);
        doSelectTooltipDialogTextBoxItem(widgetUid, index);
    }

    public void doSelectPlatform(int index) {
        def widgetName = "Platform";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);
        doSelectTooltipDialogTextBoxItem(widgetUid, index);
    }

    /* ---- AutoCompleteTextBox ---------------*/
    public void doTypeSelectProductName(String input, int index) {
        def widgetName = "ProductName";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);
        doSelectAutoCompleteTextBoxItem(widgetUid, input, index);
    }

    public void doTypeSelectGrantBlueGroup(String input, int index) {
        def widgetName = "BlueGroup";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);
        doSelectAutoCompleteTextBoxItem(widgetUid, input, index);
    }

    public void doTypeSelectGrantUser(String input, int index) {
        def widgetName = "GrantUser";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);

        def textbox = String.format("%s.TextBox", widgetUid);
        def inputTextBox = "${textbox}.InputField.InputTextBox";
        clearText inputTextBox
        pause 200
        keyType inputTextBox, input

        pause 5000
        // click option list
        def listUid = String.format("%s.PopUpList.Options", widgetUid);
        waitForElementPresent listUid, 5000
        int listSize = getListSize(listUid);

        if (index == RANDOM_INDEX) {
            index = getRandomIndex(listSize);
        }

        console.debug(listUid, index, "/", listSize);
        def optionUid = String.format("%s[%d]", listUid, index);

        mouseOver optionUid
        pause 1000

        click optionUid
        pause 500
    }

    /* ---- Input TextBox ---------------*/
    public void doInputProductName(String input) {
        def widgetName = "ProductName";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);

        doInputTextBox(widgetUid, input);
    }

    public void doInputVRMF(String input) {
        def widgetName = "VRMF";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);

        doInputTextBox(widgetUid, input);
    }

    public void doInputDocumentName(String input) {
        def widgetName = "DocumentName";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);

        doInputTextBox(widgetUid, input);
    }

    public void doInputDescription(String input) {
        def widgetName = "Description";
        def uid = String.format("%s.%s", getModuleName(), widgetName);
        type uid, input
    }

    public void doInputGrantUser(String input) {
        def widgetName = "GrantUser";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);

        // type textbox
        def textboxUid = String.format("%s.TextBox", widgetUid);
        def inputBoxUid = String.format("%s.InputField.InputTextBox", textboxUid);
        type inputBoxUid, input
        pause 500
    }

    public void doInputGrantBlueGroup(String input) {
        def widgetName = "BlueGroup";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);

        doInputTextBox(widgetUid, input);
    }

    /* ---- Radio Button ---------*/
    public void doClickAssessmentTypeEnterprise() {
        def widgetName = "AssessmentType";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);

        def radioButtonUid = String.format("%s.%s", widgetUid, "Enterprise");
        doClickRadioButton(radioButtonUid);
    }

    public void doClickAssessmentTypeMidMarket() {
        def widgetName = "AssessmentType";
        def widgetUid = String.format("%s.%s", getModuleName(), widgetName);

        def radioButtonUid = String.format("%s.%s", widgetUid, "MidMarket");
        doClickRadioButton(radioButtonUid);
    }

    /* ---- Button ---------------*/
    public void doClickSaveButton() {
        String widgetUid = String.format("%s.%s", getModuleName(), "SaveButton");
        click widgetUid
        pause 3000
    }

    public void doClickNextButton() {
        String widgetUid = String.format("%s.%s", getModuleName(), "NextButton");
        click widgetUid
        pause 500
        waitForPageToLoad 30000
    }

    public void doClickCancelButton() {
        String widgetUid = String.format("%s.%s", getModuleName(), "CancelButton");
        click widgetUid
        pause 500
        waitForPageToLoad 30000
    }

    /* ---- Bundle Operation ---------------*/
    public void doSaveIncomplete(String docName) {
        type "CQPProfile.DocumentName", docName
        pause 500
        doClickSaveButton();
    }

    public void doCreateSWGAssessment(String docName) {
        console.debug("[OfferingType]");
        doSelectOfferingType(OfferingTypeOptionIndex.OFFERING_TYPE_SWG.ordinal());
        console.debug("[Division]");
        doSelectDivision(-1);
        console.debug("[ProductName]");
        doTypeSelectProductName("ibm", -1);
        // console.debug("[VRMF]");
        // doInputVRMF("1.0.0");
        console.debug("[ProductType]");
        doSelectProductType(-1);
        console.debug("[Platform]");
        doSelectPlatform(-1);
        console.debug("[Phase]");
        doSelectPhase(-1);
        console.debug("[AssesssmentType]");
        boolean flag = RandomUtils.nextBoolean();
        if (flag) {
            doClickAssessmentTypeEnterprise();
        } else {
            doClickAssessmentTypeMidMarket();
        }
        console.debug("[Role]");
        doSelectRole(-1);
        // console.debug("[DocumentName]");
        // doInputDocumentName(docName);
        console.debug("[Description]");
        String mark = String.format("[test swg]%s", getTimestamp());
        doInputDescription(mark);
        console.debug("[SaveButton]");
        doClickSaveButton();
        pause 2000
    }

    public void doCreateSTGAssessment(String docName) {
        console.debug("[OfferingType]");
        doSelectOfferingType(OfferingTypeOptionIndex.OFFERING_TYPE_STG.ordinal());
        console.debug("[Division]");
        doSelectDivision(-1);
        console.debug("[ProductName]");
        doTypeSelectProductName("ibm", -1);
        // console.debug("[VRMF]");
        // doInputVRMF("1.0.0");
        console.debug("[ProductType]");
        doSelectProductType(-1);
        console.debug("[Platform]");
        doSelectPlatform(-1);
        console.debug("[Phase]");
        doSelectPhase(-1);
        console.debug("[AssesssmentType]");
        boolean flag = RandomUtils.nextBoolean();
        if (flag) {
            doClickAssessmentTypeEnterprise();
        } else {
            doClickAssessmentTypeMidMarket();
        }
        console.debug("[Role]");
        doSelectRole(-1);
        // console.debug("[DocumentName]");
        // doInputDocumentName(docName);
        console.debug("[Description]");
        String mark = String.format("[test stg]%s", getTimestamp());
        doInputDescription(mark);
        console.debug("[SaveButton]");
        doClickSaveButton();
        pause 2000
    }

    public void doCreateStackAssessment(String docName) {
        console.debug("[OfferingType]");
        doSelectOfferingType(OfferingTypeOptionIndex.OFFERING_TYPE_STACK.ordinal());
        console.debug("[StackName]");
        doSelectStackName(-1);
        console.debug("[Platform]");
        doSelectPlatform(-1);
        console.debug("[Role]");
        doSelectRole(-1);
        // console.debug("[DocumentName]");
        // doInputDocumentName(docName);
        console.debug("[Description]");
        String mark = String.format("[test stack]%s", getTimestamp());
        doInputDescription(mark);
        console.debug("[SaveButton]");
        doClickSaveButton();
        pause 2000
    }

    public void doCreateComponentAssessment(String docName) {
        console.debug("[OfferingType]");
        doSelectOfferingType(OfferingTypeOptionIndex.OFFERING_TYPE_COMPONENT.ordinal());
        console.debug("[Division]");
        doSelectDivision(-1);
        console.debug("[ProductName]");
        doTypeSelectProductName("ibm", -1);
        // console.debug("[VRMF]");
        // doInputVRMF("1.0.0");
        console.debug("[ProductType]");
        doSelectProductType(-1);
        console.debug("[Platform]");
        doSelectPlatform(-1);
        console.debug("[Phase]");
        doSelectPhase(-1);
        console.debug("[AssesssmentType]");
        boolean flag = RandomUtils.nextBoolean();
        if (flag) {
            doClickAssessmentTypeEnterprise();
        } else {
            doClickAssessmentTypeMidMarket();
        }
        console.debug("[Role]");
        doSelectRole(-1);
        // console.debug("[DocumentName]");
        // doInputDocumentName(docName);
        console.debug("[Description]");
        String mark = String.format("[test component]%s", getTimestamp());
        doInputDescription(mark);
        console.debug("[SaveButton]");
        doClickSaveButton();
        pause 2000
    }

    public void doCreatePreAssessment(String docName) {
        console.debug("[Role]");
        doSelectRole(-1);
        console.debug("[DocumentName]");
        def documentName = getTextBoxValue("DocumentName");
        documentName = String.format("%s_[%s]", documentName, docName);
        doInputDocumentName(documentName);
        console.debug("[Description]");
        String mark = String.format("[test preliminary]%s", getTimestamp());
        doInputDescription(mark);
        console.debug("[SaveButton]");
        doClickSaveButton();
        pause 2000
    }
}
